'use strict'

const { questionnaire, questionnaireAnswers, QSSelect } = require('../models')

//卡在这（想获取用户点击的是哪个表单Id，然后根据表单Id将数据查询出来并打印到页面）
// 调查问卷页面
var fn_QS1 = async (ctx, next) => {
  //用户名
  var username = ctx.session.userName;
  //用户id
  var userId = ctx.session.userId;
  //通过当前用户点击前往的问卷Id 查找 用户点击前往的问卷
  //获取当前用户Id所点击的问卷Id
  var userClickQs = await QSSelect.findAll({
    order: [
      ['id', 'desc']
    ],
    where: {
      userId: userId
    }
  });
  // console.log(userClickQs);
  var questionnaireId = userClickQs[0].questionnaireId;

  // 获取与点击前往的问卷id相等的问卷
  var userClickQs2 = await questionnaire.findOne({
    where: {
      id: questionnaireId
    }
  });

  // console.log(userClickQs2);


  //问卷编号
  var qsId = userClickQs2.id;
  //问卷发布人
  var publishUserId = userClickQs2.publishUserId;
  //问卷名
  var qsName = userClickQs2.qsName;
  //问卷简介
  var qsSynopsis = userClickQs2.qsSynopsis;
  //问卷题目

  var topicOne = userClickQs2.topicOne;
  var topicTwo = userClickQs2.topicTwo;
  var topicThree = userClickQs2.topicThree;
  var topicFour = userClickQs2.topicFour;
  var topicFive = userClickQs2.topicFive;
  var topicSix = userClickQs2.topicSix;
  var topicSeven = userClickQs2.topicSeven;
  var topicEight = userClickQs2.topicEight;
  var topicNine = userClickQs2.topicNine;
  var topicTen = userClickQs2.topicTen;

  //问卷创建时间
  var createdAt = userClickQs2.createdAt;
  // console.log(qsId);
  // console.log(publishUserId);
  // console.log(qsName);
  // console.log(qsSynopsis);
  // console.log(topicFive);

  var obj = {
    //问卷编号
    qsId,
    //问卷发布人
    publishUserId,
    //问卷名
    qsName,
    //问卷简介
    qsSynopsis,
    //问卷题目
    topicOne,
    topicTwo,
    topicThree,
    topicFour,
    topicFive,
    topicSix,
    topicSeven,
    topicEight,
    topicNine,
    topicTen,
    //用户名称
    username,
    //问卷创建时间
    createdAt
  }
  ctx.render("QS1.html", obj)

};

//用户点击的是哪个表单判断
var fn_QSJudge = async (ctx, next) => {

  //获取用户点击前往的问卷id
  var qsId = ctx.request.body.qoeId.trim();
  //获取点击用户Id
  var userId = ctx.session.userId;

  //将点击前往的问卷id与点击用户Id存入数据库
  var QSSelects = await QSSelect.create({
    questionnaireId: qsId,
    userId: userId
  });


  let res = "";
  res = { code: 200 };
  ctx.body = JSON.stringify(res);
};
//

// 问卷一表单提交
var fn_QS1Judge = async (ctx, next) => {

  //问卷一答题人Id
  var questionUserId = ctx.session.userId;

  //问卷一答题人答案
  var question1 = ctx.request.body.Question1;
  var question2 = ctx.request.body.Question2;
  var question3 = ctx.request.body.Question3;
  var question4 = ctx.request.body.Question4;
  var question5 = ctx.request.body.Question5;
  var question6 = ctx.request.body.Question6;
  var question7 = ctx.request.body.Question7;
  var question8 = ctx.request.body.Question8;
  var question9 = ctx.request.body.Question9;
  var question10 = ctx.request.body.Question10;

  //问卷Id
  var qsId = ctx.request.body.qsId;

  // console.log(question1);
  // console.log(questionUserId);
  // console.log(qsId);


  // 向questionnaireAnswers答案表存答案
  var questionnaireAnswerss = await questionnaireAnswers.create({
    questionnaireId: qsId,
    answerUserId: questionUserId,
    topicOneAnswer: question1,
    topicTwoAnswer: question2,
    topicThreeAnswer: question3,
    topicFourAnswer: question4,
    topicFiveAnswer: question5,
    topicSixAnswer: question6,
    topicSevenAnswer: question7,
    topicEightAnswer: question8,
    topicNineAnswer: question9,
    topicTenAnswer: question10,
  })


  let res = "";
  res = { code: 200, msg: "参与调查问卷成功" };
  ctx.body = JSON.stringify(res);
};

// 问卷提交成功页面
var fn_QS1Ok = async (ctx, next) => {
  //提交成功
  var obj = {
    // 网页头像
    icon: "/statics/img/QSOkicon.png",
    // 网页插图
    inbetweening: "/statics/img/QSOk.png",
    // 网页标题
    title: "提交成功",
    // 错误内容
    OkContent: "恭喜你参加调查问卷已完成！",
    // 按钮一路径
    router1: '/home/SQ1',
    // 按钮一文本
    button1: "再填写一次",
    // 按钮二路径
    router2: '/home',
    // 按钮二文本
    button2: "参与其它问卷"
  }
  ctx.render('QSOk.html', obj)
};


module.exports = {
  '/home/SQ1': ['get', fn_QS1],
  '/home/SQ1Ok': ['get', fn_QS1Ok],
  '/home/SQJudge': ['post', fn_QSJudge],
  '/home/SQ1judge': ['post', fn_QS1Judge]
}